<?php

include "../../site_specific/project.php";
include "../../includes/constants.php";
include "../../site_specific/external_constants.php";
include "../../includes/phpmailer/class.phpmailer.php";
include "../../includes/phpmailer/class.smtp.php";
include "../../includes/utilities.php";
include "../../includes/pages.php";
include "../../includes/database.php";
include "../../includes/users.php";
include "../../includes/date.php";
include "../../includes/text.php";
include "../../includes/files.php";

# Establish database connection
establishDatabaseConnection();

#Get the parent
$parent=$_GET["q"];

#Get the user permissions
$user=$_GET["u"];

#Select all the pages
$sql_getall="SELECT * FROM ".$PROJECT_DB_TABLES['pages']." WHERE p_section != 'newsletters' ORDER BY p_sort ASC, p_name ASC";

#Get data		
$current_data=getRecord($sql_getall);

#Output datatable if the selected item is not "SELECT ALL"
if($parent!=""){		
		#Current section variable
		$p_section="";
		
		#Current page data variable
		$current_page_tmp=array();
		
		#Get selected page details
		foreach($current_data as $current_data_key=>$current_data_value){
			
			if($current_data_value["p_id"]==$parent){
				
				#Get the page details if the current page is the selected page
				$current_page_tmp=$current_data_value;
				
				#If current page is not in the junk box
			  	if($current_data_value["p_status"]<3){
					
					#If the selected page section is admin, the section will be admin,otherwise it is site.
					if($current_data_value["p_section"]=="admin"){
						  $p_section="admin";
					}
					else if($current_data_value["p_section"]=="site"){
						  $p_section="site";				
					}
			  	}
			}
		}
		
		#output the table caption
		echo "<table class=\"admintable\" cellspacing=\"0\">" .
			"<thead><tr><th>Page</th><th>Page Status</th><th>Move</th><th>Preview</th><th>Content</th><th>Actions</th></tr>			</thead>" ."<tbody>";
			
		if($p_section=="site"){
			 echo "<tr class=\"rowsubheader\"><td colspan=\"6\"><strong>SITE</strong></td></tr>";
			echo "<tr>" ."<td>". "<a href=\"../" . $current_page_tmp["p_section"] . "/" . $current_page_tmp["p_id"] . ".php\">" . $current_page_tmp["p_name"] . "</a> (" . $current_page_tmp["p_id"] . ")</td>" ."<td>" . getPageStatusName($current_page_tmp["p_status"]) . "</td>" ."<td><a href=\"javascript:launchPageMove('" . $current_page_tmp["p_id"] . "');\">Move</a></td>" ."<td><a href=\"javascript:launchPagePreview('" . $current_page_tmp["p_id"] . "');\">Preview</a></td>" .
					"<td><a href=\"javascript:editPageContent('" . $current_page_tmp["p_id"] . "');\">Content</a></td>" .
					"<td><a href=\"javascript:doAction('edit', '" . $current_page_tmp["p_id"] . "', false);\">Edit</a> / <a href=\"javascript:doAction('remove', '" . $current_page_tmp["p_id"] . "', true);\">Remove</a></td></tr>";
		 
  
		  adminPageFilterPrintOut($current_data, $parent, "site", 1);
		
		}
		
		else if($p_section=="admin"){
			echo "<tr class=\"rowsubheader\"><td colspan=\"6\"><strong>Admin</strong></td></tr>";
			 
			echo "<tr>" ."<td>". "<a href=\"../" . $current_page_tmp["p_section"] . "/" . $current_page_tmp["p_id"] . ".php\">" . $current_page_tmp["p_name"] . "</a> (" . $current_page_tmp["p_id"] . ")</td>" ."<td>" . getPageStatusName($current_page_tmp["p_status"]) . "</td>" ."<td><a href=\"javascript:launchPageMove('" . $current_page_tmp["p_id"] . "');\">Move</a></td>" ."<td><a href=\"javascript:launchPagePreview('" . $current_page_tmp["p_id"] . "');\">Preview</a></td>" .
					"<td><a href=\"javascript:editPageContent('" . $current_page_tmp["p_id"] . "');\">Content</a></td>" .
					"<td><a href=\"javascript:doAction('edit', '" . $current_page_tmp["p_id"] . "', false);\">Edit</a> / <a href=\"javascript:doAction('remove', '" . $current_page_tmp["p_id"] . "', true);\">Remove</a></td></tr>";
					
		 
  
		  adminPageFilterPrintOut($current_data, $parent, "admin", 1);
		
		}

		echo "</tbody></table>";
				

}
#for all
else{

		$ROW_COUNT = 0; 

		echo "<table class=\"admintable\" cellspacing=\"0\">" .
			"<thead><tr><th>Page</th><th>Page Status</th><th>Move</th><th>Preview</th><th>Content</th><th>Actions</th></tr></thead>" .
			"<tbody>";

		echo "<tr class=\"rowsubheader\"><td colspan=\"6\"><strong>SITE</strong></td></tr>";

		adminPageFilterPrintOut($current_data, "", "site", 0);

		echo "<tr class=\"rowsubheader\"><td colspan=\"6\"><strong>Admin</strong></td></tr>";

		adminPageFilterPrintOut($current_data, "", "admin", 0);
		
		
		if($user==3){
		
		echo "<tr class=\"rowsubheader\"><td colspan=\"6\"><strong>Junk Box</strong></td></tr>";

		adminPageFilterJunkPrintOut($current_data);
		}

		echo "</tbody></table>";

}

function adminPageFilterJunkPrintOut($current_data){
	foreach ($current_data as $current_page_id => $current_page) {

		if ($current_page["p_status"]==3) {
	
				echo "<tr>" .
					"<td>".$current_page["p_id"]."</td>" .
					"<td>" . getPageStatusName($current_page["p_status"]) . "</td>" .
					"<td><a href=\"javascript:doAction('restore', '" . $current_page_id . "', false);\">Restore</a></td></tr>";		
		}
	}
}

function adminPageFilterPrintOut($current_pages = array(), $parent_id = "", $section = "site", $depth = 0) {

	global $ROW_COUNT;
	if (!$ROW_COUNT) {
		$ROW_COUNT = 0;
	}

	foreach ($current_pages as $current_page_id => $current_page) {

		if ($current_page["p_parent"] == $parent_id && $current_page["p_section"] == $section && $current_page["p_status"]<3) {
				$padding = "";
				for ($i = 0; $i < $depth; $i++) {
					$padding .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
	
				echo "<tr" . ($ROW_COUNT % 2 == 0 ? " class=\"alternate\"" : "") . ">" .
					"<td>" . $padding . "<a href=\"../" . $current_page["p_section"] . "/" . $current_page["p_id"] . ".php\">" . $current_page["p_name"] . "</a> (" . $current_page["p_id"] . ")</td>" .
					"<td>" . getPageStatusName($current_page["p_status"]) . "</td>" .
					"<td><a href=\"javascript:launchPageMove('" . $current_page["p_id"] . "');\">Move</a></td>" .
					"<td><a href=\"javascript:launchPagePreview('" . $current_page["p_id"] . "');\">Preview</a></td>" .
					"<td><a href=\"javascript:editPageContent('" . $current_page["p_id"] . "');\">Content</a></td>" .
					"<td><a href=\"javascript:doAction('edit', '" . $current_page["p_id"] . "', false);\">Edit</a> / <a href=\"javascript:doAction('remove', '" . $current_page["p_id"] . "', true);\">Remove</a></td></tr>";
	
				$ROW_COUNT++;
	
				#addContent("<p>" . $depth . ": Page: " . $current_page_id . " (Parent: " . $current_page["p_parent"] . ")</p>");
	
				adminPageFilterPrintOut($current_pages, $current_page["p_id"], $section, $depth + 1);
		}
	}
}




?>